// pages/myinfo/myinfo.js
var app = getApp();
var c = require("../common.js");
var upimg = require('../upimg.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    widthinfo: '100rpx',
    date: '2016-09-01',
    sex: ['男', '女'],
    index: 0,
    region: ['北京市', '北京市', '东城区']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var fileurl = app.testimgurl;
    var uid = app.u.user_id;
    var that = this;
    this.setData({
      fileurl: fileurl
    });
    c.ajax(
      app.url + 'myinfo', {
        user_id: uid
      },
      res => {
        var address = res.data.address;
        console.log(address);
        var region = address.split("-");
        if(res.data.sex=='女'){
          var index=1
        }else{
          var index=0
        };
        that.setData({
          index:index,
          data: res.data,
          date: res.data.birthday,
          region: region,
          image: res.data.image
        })
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //昵称
  blur: function(e) {
    var value = e.detail.value;
    var uid = app.u.user_id;
    c.ajax(
      app.url + 'setmyinfo', {
        type: 2,
        value: value,
        uid: uid
      },
      res => {
        if (res.data == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        }
      }
    )
  },
  //性别
  bindPickerChange: function(e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为1', e.detail.value)
    var index = e.detail.value;
    this.setData({
      index: e.detail.value
    })
    if (index == 0) {
      var value = '男';
    } else {
      var value = '女';
    }
    var uid = app.u.user_id;
    c.ajax(
      app.url + 'setmyinfo', {
        type: 3,
        value: value,
        uid: uid
      },
      res => {
        if (res.data == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        }
      }
    )

  },
  //日期
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为2', e.detail.value)
    var value = e.detail.value;
    this.setData({
      date: e.detail.value
    })
    console.log(this.data);
    var uid = app.u.user_id;
    c.ajax(
      app.url + 'setmyinfo', {
        type: 4,
        value: value,
        uid: uid
      },
      res => {
        if (res.data == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        }
      }
    )
  },
  //地区
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为3', e.detail.value)
    var address = e.detail.value;
    var value = address.join("-");
    var uid = app.u.user_id;
    this.setData({
      region: e.detail.value
    })
    c.ajax(
      app.url + 'setmyinfo', {
        type: 5,
        value: value,
        uid: uid
      },
      res => {
        if (res.data == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        }
      }
    )
  },
  //个人简介
  blurint: function(e) {
    var value = e.detail.value;
    var uid = app.u.user_id;
    c.ajax(
      app.url + 'setmyinfo', {
        type: 6,
        value: value,
        uid: uid
      },
      res => {
        if (res.data == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        }
      }
    )
  },
  simpleUpload: function(e) {
    var path = 'user/' + new Date().getTime() + '.png';
    var that = this;
    var uid = app.u.user_id;
    upimg.simpleUpload(
      path,
      function(res) {
        var image = res[0]['dbs'];
        c.ajax(
          app.url + 'setmyinfo', {
            type: 1,
            value: '/' + path,
            uid: uid
          },
          res => {
            setTimeout(function() {
              that.setData({
                image: image
              });
            }, 1000)
            if (res.data == 1) {
              wx.showToast({
                title: '修改成功',
                icon: 'none'
              })
            }
          }
        )


        that.setData({
          arrayimg: res
        })
      },
      6
    );
  },
  clickfuzhi: function(res) {
    var info = this.data;
    var phone = info.data.phone;
    var stringinfo = 'http://www.wyueke.com?intivi=' + phone;
    wx.setClipboardData({
      data: stringinfo,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            wx.showToast({
              title: '复制成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  }
})